package com.phcx.corporate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.phcx.corporate.model.BizLogContent;
import com.phcx.corporate.model.UserLogContent;
import com.phcx.corporate.service.BizLogContentService;
import com.phcx.corporate.service.UserLogContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/*
 * @Author: zmh
 * @Date: 2023/06/05/10:12
 * @Description: 定时清理报文记录
 *
 * 清理周期：每天清理一次，清理一个月之前的报文记录
 *
 * 注意：项目启动时会自动清理一次
 */
@Controller
@Slf4j
@EnableScheduling
public class TimingCleaningController {

    @Value("${userLogContentTimeout}")
    private Long userLogContentTimeout;

    @Value("${bizLogContentTimout}")
    private Long bizLogContentTimeout;


    @Autowired
    private BizLogContentService bizLogContentService;

    @Autowired
    private UserLogContentService userLogContentService;


    /**
     *  用户更改前后对比日志记录清理
     */
    @Scheduled(fixedRate = 12 * 60 * 60 * 1000)
    public void userLogContentClean(){
        LocalDateTime startTime = LocalDateTime.now().minusDays(userLogContentTimeout);
        String startId = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(startTime);
        QueryWrapper<UserLogContent> wrapper = new QueryWrapper<>();
        wrapper.lt("log_id",startId);
        userLogContentService.remove(wrapper);
        log.info("[用户前后对比日志已清理]");
    }

    /**
     *  调用省局接口，查询企业码的报文记录
     */
    @Scheduled(fixedRate = 12 * 60 * 60 * 1000)
    public void bizLogContentClean(){
        LocalDateTime startTime = LocalDateTime.now().minusDays(bizLogContentTimeout);
        String startId = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(startTime);
        QueryWrapper<BizLogContent> wrapper = new QueryWrapper<>();
        wrapper.lt("log_id",startId);
        bizLogContentService.remove(wrapper);
        log.info("[查询企业码报文记录已清理]");
    }

}
